package com.leyou.item.controller;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.Spu;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import jdk.nashorn.internal.runtime.logging.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class GoodsController {

    private final SpuService spuService;

    private final SpuDetailService detailService;

    private final SkuService skuService;

    public GoodsController(SpuService SpuService, SpuDetailService detailService, SkuService skuService) {
        this.spuService = SpuService;
        this.detailService = detailService;
        this.skuService = skuService;
    }

    /**
     *
     * @param page  当前页
     * @param rows  每页大小
     * @param saleable 上下架的商品
     * @param categoryId 分类id
     * @param brandId 品牌id
     * @param id spu的id
     * @return  当前页商品数据
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "id", required = false) Long id) {
        return ResponseEntity.ok(spuService.querySpuByPage(page, rows, saleable, categoryId, brandId, id));
    }

    /**
     * 新增商品
     * @param spuDTO  页面提交的商品信息
     * @return
     */
    @PostMapping("spu")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        spuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSpuSaleable(
            @RequestParam ("id") Long id,@RequestParam("saleable") boolean saleable){
        spuService.updateSaleable(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id查询商品信息
     * @param id 商品的id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id){
         SpuDTO spuDTO= spuService.queryGoodsById(id);
          return ResponseEntity.ok(spuDTO);
    }

    /**
     * 修改商品
     * @param spuDTO 页面提交商品信息
     * @return
     */
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        //修改商品
        spuService.updateGoods(spuDTO);
        //成功,返回204
        return ResponseEntity.noContent().build();
    }

    /**
     * 根据id查询detail
     * @param id 商品id
     * @return SpuDetail
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id")Long id){
        //1.根据id查询detail
        SpuDetail spuDetail = detailService.getById(id);
        //2.转为DTO返回
        return  ResponseEntity.ok(new SpuDetailDTO(spuDetail));
    }

    /**
     * 根据SpuId查询spu
     * @param id spuId
     * @return sku集合
     */
    @GetMapping("/sku/of/spu")
    public  ResponseEntity<List<SkuDTO>> qureySkuBySpuId(@RequestParam("id")Long id){
        return  ResponseEntity.ok(SkuDTO.convertEntityList(skuService.query().eq("spu_id",id).list()));
    }

    /**
     * 根据id批量查询sku
     * @param ids id的集合
     * @return  SkuDTO集合
     */
    @GetMapping("/sku/list")
    public  ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuService.listByIds(ids)));
    }

    /**
     * 根据spuId查询spu的所有规格参数值
     * @param id spuId
     * @param searching  是否参与搜索
     * @return 规格参数键值对集合
     */
    @GetMapping("/spec/value")
    public  ResponseEntity<List<SpecParamDTO>>querySpecsValues(
            @RequestParam("id")Long id,@RequestParam(value = "searching",required = false)Boolean searching){
        List<SpecParamDTO> params= spuService.querySpecValue(id,searching);
        return ResponseEntity.ok(params);
    }

    /**
     * 根据id查询商品
     * @param id 商品id
     * @return 商品信息
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id")Long id){
        //1.通过id查询商品
        Spu spu = spuService.getById(id);
        //2.转为DTO返回 ok
        return  ResponseEntity.ok(new SpuDTO(spu));
    }
 }